package com.swd.order.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.swd.order.mapper.FoodsMapper;
import com.swd.order.mapper.FoodsSlaveMapper;
import com.swd.order.model.Foods;
import com.swd.order.model.FoodsExample;
import com.swd.order.model.FoodsSlaveExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class FoodsService {
    @Autowired
    private FoodsMapper foodsMapper;
    @Autowired
    private FoodsSlaveMapper foodsSlaveMapper;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Foods> queryInIdList(List<Integer> list){
        FoodsExample foodsExample = new FoodsExample();
        foodsExample.createCriteria().andIdIn(list);
        return  foodsMapper.selectByExample(foodsExample);
    }

    public Foods queryById(int id){
        FoodsExample foodsExample = new FoodsExample();
        foodsExample.createCriteria().andIdEqualTo(id);
        List<Foods> foods = foodsMapper.selectByExample(foodsExample);
        return  foods.size()>0?foods.get(0):null;
    }

    public boolean releaseCount(List<Foods> foods) {
        boolean success = true;

        for (Foods food : foods) {
            int i = foodsMapper.updateByPrimaryKeySelective(food);
            success = success && i == 1;
        }


        if (success){
            return true;
        }
        return false;
    }

    public boolean save(Foods foods){
        int i = foodsMapper.insertSelective(foods);
        return i == 1;
    }


    public List<Foods> queryByName(String name){
        FoodsExample foodsExample = new FoodsExample();
        foodsExample.createCriteria().andNameEqualTo(name);
        List<Foods> foodsList = foodsMapper.selectByExample(foodsExample);
        return foodsList;
    }

    public boolean update(Foods foods) {
        int i = foodsMapper.updateByPrimaryKeySelective(foods);
        return i == 1;
    }

    public Page<Foods> query(Integer categoryId, String name,int pageNum,int limit) {

        Page<Foods> page = PageHelper.startPage(pageNum,limit);

        foodsMapper.queryFoodsWithSlave(categoryId,name);

        return page;
    }

    public boolean delete(int id) {

        int i = foodsMapper.deleteByPrimaryKey(id);

        return i == 1 ;
    }


    public boolean foodsCountIncrease(List<Integer> foodsIdList){

        int effect = foodsMapper.foodsCountIncrease(foodsIdList);

        if (effect != foodsIdList.size()){
            return false;
        }

        return true;
    }

    public List<Foods> queryAll() {
        FoodsExample foodsExample = new FoodsExample();
        foodsExample.createCriteria().andSetMealEqualTo(false);
        return foodsMapper.selectByExample(foodsExample);

    }

    public boolean saveSetMeal(Foods food, List<Integer> idList) {

        FoodsExample foodsExample = new FoodsExample();
        foodsExample.createCriteria().andIdIn(idList);

        List<Foods> foodsList = foodsMapper.selectByExample(foodsExample);
        if (foodsList.size() != idList.size()){
            return false;
        }

        int i = foodsMapper.insertSelective(food);

        int j = foodsMapper.insertSetMeal(food.getId(),idList);

        return i == 1 && j == idList.size();
    }

    public boolean updateItem(Integer id, List<Integer> idList) {

        FoodsSlaveExample foodsSlaveExample = new FoodsSlaveExample();

        foodsSlaveExample.createCriteria().andFoodMasterIdEqualTo(id);

        int i = foodsSlaveMapper.deleteByExample(foodsSlaveExample);

        int j = foodsMapper.insertSetMeal(id,idList);

        return j == idList.size();
    }

    public void sort(List<Integer> idSortList) {
        int i = foodsMapper.batchUpdateIndex(idSortList);
        System.out.println("i = " + i);
    }



}
